# coding: utf8

from gluon.html import *
import gettext
_ = gettext.gettext

__all__ = [ 'Button', 'Dialog', 'ErrorMessage', 'Grid', 'Help' ]


def ui_button (xid, xlabel = '', xicon = ''):
    """
    Rendor HTML stub for a jQuery button.
    """
    xl = _(xlabel)
    xtitle = _(xlabel if xlabel else xid)
    if xtitle: xtitle = " title='%s'"%xtitle
    if xicon != '': xicon = "<span class='ui-icon ui-button-icon-primary ui-icon-%s'></span>" % xicon
    return "<button id='c9r-button-%s'%s>%s%s</button>" % (xid, xtitle, xicon, xl)

def ui_dialog (xid):
    return "<div id='c9r-dialog-%s'></div>" % xid;

def ui_error_message (xmsg):
    return "<div class='ui-state-error'>"+_(xmsg)+"</div>";

def ui_grid(xid, xclear = False, xpager = True):
    """
    Return a jQuery Grid HTML skeleton.

    @param	xid	ID of the grid
    @param	xclear	{both, left, right} to clear HTML block
    @param	xpager	True to include a pager in the grid
    """
    xhtml = (("<br clear='%s' style='margin: 0 0 1em 0;' />" % xclear) if xclear else '')\
        + ("<table id='%s-grid' class='scroll' cellpadding='0' cellspacing='0'></table>" % xid)
    if xpager:
        xhtml += ("<div id='%s-pager' class='scroll' style='text-align:center;'></div>" % xid)
    return xhtml

def ui_help (xid):
    return ui_button(xid+'-help', 'Help')

def ui_inline (xtxt, xid = '', xdisplay = True):
    return '<span' + ('' if xid == '' else (" id='%s'" % xid))\
                + " display='" + (xdisplay if isinstance(xdisplay, str) else
                                  ('inline' if xdisplay else 'none'))\
                + ("'>%s</span>" % xtxt)

def ui_list (xarr, xul = 'ul'):
    if xul != 'ul': xul = 'ol'
    return ("<%s><li>" % xul) + "</li><li>".join(xarr) + ("</li></>" % xul)

def ui_pulldown (xid, xopts, xselect = False):
    xkeyed = isinstance(xopts, dict)
    xhtml = "<select id=''>" % xid
    for xkey in xopts:
        xval = xopts[xkey]
        xhtml += "<option value='%s'>%s</option>" % (xkey if xkeyed else xval, xval)
	return xhtml+'</select>'

def ui_toolbar (xbutton):
    """
    Generate HTML code for a tool bar with jQuery UI buttons.
    """
    xhtml = '';
    for xbtn in xbutton:
        xhtml += (ui_button(xbtn[0], xbtn[1], xbtn[2]) if isinstance(xbtn, list)
                  else ("<span style='padding:%s\px;'></span>" % xbtn if isinstance(xbtn, int)
                        else ("<span style='padding:%s\em;'></span>" % xbtn if isinstance(xbtn, float)
                              else ui_button(xbtn))))
        return xhtml

class Button(DIV):
    tag = 'button'
    def __init__(self, id, label = '', icon = ''):
        xl = _(label)
        super(Button, self).__init__(xl, _id="c9r-button-"+id)
        title = _(label if label != '' else id)
        if title: self.attributes['_title'] = title
        if icon != '': self.insert(0, SPAN(_class='ui-icon ui-button-icon-primary ui-icon-%s' % icon))

class Dialog(DIV):
    def __init__(self, id):
        super(Dialog, self).__init__(_id="c9r-dialog-"+id)

class ErrorMessage(DIV):
    def __init__(self, xmsg):
        super(ErrorMessage, self).__init__(_(xmsg), _class='ui-state-error')

class Grid(DIV):
    def __init__(self, xid, xclear = False, xpager = True):
        """
        Return a jQuery Grid HTML skeleton.

        @param	xid	ID of the grid
        @param	xclear	{both, left, right} to clear HTML block
        @param	xpager	True to include a pager in the grid
        """
        super(Grid, self).__init__(_id="c9r-grid-div-"+xid)
        if xclear: self.append(BR(_clear=xclear,_style='margin: 0 0 1em 0;'))
        self.append(TABLE(_id='c9r-grid-'+xid,_class='scroll',_cellpadding='0',_cellspacing='0'))
        if xpager: self.append(DIV(_id='c9r-pager-'+xid,_class='scroll',_style='text-align:center;'))

class Help(Button):
    def __init__(self, xid=''):
        super(Help, self).__init__(xid+'-help', 'Help', 'help')
